/**
 * Created by Administrator on 2017/4/19.
 *
*method 请求方法,GET或POST
*url 请求的URL
*data 发送的数据，如:'name=james&age=22'
*success 成功时的回调函数，该函数可接受一个参数，即服务器返回的数据
*fail 失败时的回调函数，该函数可接受一个参数，即服务器的返回状态码
*/

function AJAX(method,url,data,success,fail)
{
    var ajax=new XMLHttpRequest();
    ajax.onreadystatechange=function(){
        if(ajax.readyState == 4)
        {
            if(ajax.status == 200 || ajax.status == 201 || ajax.status == 304)
            {
                if(success)
                {
                    success(ajax.responseText);
                }
            }
            else
            {
                if(fail)
                {
                    fail(ajax.status);
                }
            }
        }
    };
    if(method=="POST" || method=="post")
    {
        ajax.open("POST",url);
        ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        ajax.send(data);
    }
    else if(method=="GET" || method=="get")
    {
        ajax.open("GET",url+"?"+data);
        ajax.send(null);
    }
    else
    {
        alert("error method");
    }
    return ajax;
}
function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r != null) return unescape(r[2]);
    return null;
}

AJAX(
    "post",
    "http://xywx.sxxyyj.com/wechat/get_oppend",
    "code="+GetQueryString("code"),
    function(ID) {
        console.log("success"+ID)
        localStorage.setItem("openId", JSON.parse(ID).openid)
    },
    function(ID) {
        console.log("error"+ID)
        localStorage.setItem("openId", ID.openid)
    }
)